package com.ryskoo.controller.promotion;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ryskoo.catering.coupondetail.domain.CouponDetailPO;
import com.ryskoo.catering.coupondetail.service.ICouponDetailService;
import com.ryskoo.common.hclconstant.CouponStateEnum;
import com.ryskoo.common.hclconstant.CouponTypeEnum;
import com.ryskoo.framework.core.bean.EnumConvertParam;
import com.ryskoo.framework.core.bean.Pager;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.bean.ReturnCodeEnum;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.utils.LoginUtil;

@Controller("caterCouponDetailController")
@RequestMapping("/vendor/cater/coupondetail")
public class CouponDetailController extends BasicHandler<CouponDetailPO> {

    private static final String TEMPLATE_PATH = "system/promotion";

    private static final String MODULE_NAME = "CouponDetail";
    /**
     * 基类构造方法.
     */
    public CouponDetailController() {
        super(TEMPLATE_PATH, MODULE_NAME);
    }

    private ICouponDetailService couponDetailService;

    

    /**
     * 消费者注入
     *
     * @param productService 消费者基类
     */
    @Autowired
    protected void setService(ICouponDetailService couponDetailService) {
        this.couponDetailService = couponDetailService;
        super.setService(couponDetailService);
    }

    
    @PostMapping("/cancel")
    @ResponseBody
    public ReturnMessageDTO cancel(@RequestBody String uuid) {
        ReturnMessageDTO<List<CouponDetailPO>> returnMessage = new ReturnMessageDTO<>();
        CouponDetailPO po = couponDetailService.getByUuid(uuid);
        po.setState(CouponStateEnum.USED.getValue());
        couponDetailService.update(po);
        returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
        returnMessage.setReturnNote("查询成功");
        return returnMessage;
    }
    
    @GetMapping("/tocustomercoupon/{customerId}")
	public String toView(@PathVariable(value = "customerId") String customerId, Model model,HttpServletRequest request) {
        model.addAttribute("customerId", customerId);
        String vendorId = LoginUtil.getLoginVendorUuid();
		model.addAttribute("vendorId", vendorId);
		// 类型
		List<EnumConvertParam> couponTypeList = new ArrayList<>();
		for(CouponTypeEnum o : CouponTypeEnum.values()){
			EnumConvertParam param = new EnumConvertParam();
			param.setValue(o.getValue());
			param.setName(o.getName());
			couponTypeList.add(param);
		}
		model.addAttribute("couponTypeList", couponTypeList);
		// 状态
		List<EnumConvertParam> stateList = new ArrayList<>();
		for(CouponStateEnum o : CouponStateEnum.values()){
			EnumConvertParam param = new EnumConvertParam();
			param.setValue(o.getValue());
			param.setName(o.getName());
			stateList.add(param);
		}
		model.addAttribute("stateList", stateList);
		
        return "system/promotion/CustomerCouponList";
	}

    /**
     * 分页查询优惠券列表，带商品名称
     * @param qm
     * @return
     */
    @PostMapping("/listAndProductName")
    @ResponseBody
    public ReturnMessageDTO<List<CouponDetailPO>> listAndProductName(@RequestBody QueryMessageDTO qm) {
        ReturnMessageDTO<List<CouponDetailPO>> returnMessage = new ReturnMessageDTO<>();
        List<CouponDetailPO> list = couponDetailService.queryListAndProductByCondition(qm);
        int count = couponDetailService.getCount(qm);
        returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
        returnMessage.setReturnNote("查询成功");
        returnMessage.setReturnMessage(list);
        returnMessage.setPager(new Pager(qm.getPageShow(), qm.getStartNum(), count));
        return returnMessage;
    }


	@Override
	protected void buildListPageData(Model model, HttpServletRequest request) {
		String vendorId = LoginUtil.getLoginVendorUuid();
		model.addAttribute("vendorId", vendorId);
		String couponUuid = request.getParameter("couponUuid");
		model.addAttribute("couponUuid", couponUuid);
	}
    
    
}
